<template>
    <div>
        <el-dialog :title="form.id ? '编辑请假悉尼下' : '新建请假信息'" width="700px" :visible.sync="dialogVisible">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col  :span="24">
                        <el-form-item label="员工">
                            <el-cascader v-model="form.user_id" :options="departOptions" :props="{value: 'id', label: 'realname'}" :show-all-levels="false" style="width: 100%;" @change="deptChange" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="请假开始时间">
                            <el-date-picker
                                v-model="form.start_time"
                                type="datetime"
                                placeholder="请选择">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="请假结束时间">
                            <el-date-picker
                                v-model="form.end_time"
                                type="datetime"
                                placeholder="请选择">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { saveData } from '@/api/holiday'
import { departList, memberList } from '@/api/system'
import { deepClone } from '@/util'
export default {
    name: 'ResvervationAddDialog',
    data() {
        return {
            dialogVisible: false,
            departOptions: [],
            userDisabled: false,
            form: {
                id: 0,
                user_id: '',
                start_time: '',
                end_time: ""
            },
            rules: {
            }
        }
    },
    methods: {
        showDialog(item) {
            this.dialogVisible = true;
            this.resetParams();
            this.resetForm();
            this.getMemberList();
            if (item && item.id) {
                this.form = deepClone(item)
            }
            if (item.user_id) {
                this.$nextTick(() => {
                    this.form.user_id = item.user_id;
                    this.userDisabled = true
                })
            }
        },
        resetForm() {
            this.$nextTick(() => {
                this.$refs.ruleForm.resetFields();
            })
        },
        resetParams() {
            this.form = {
                id: 0,
                user_id: '',
                start_time: '',
                end_time: ""
            };
            this.userDisabled = false;
        },
        handleSubmit(e) {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    saveData(this.form).then(res => {
                        if (res.status == '1') {
                            this.$message.success('操作成功！')
                            this.$emit('success', res)
                        } else {
                            this.$message.error(res.message)
                        }
                    }).finally(() => {
                        this.dialogVisible = false
                    })
                }
            })
        },
        getMemberList() {
            memberList().then(res => {
                if (res.status == '1') {
                    this.departOptions = res.data.list || []
                }
            })
        },
        deptChange(e) {
            this.$nextTick(() => {
                this.form.user_id = e.length ? e[e.length - 1] : ''
            })
        }
    }
}
</script>
